/*
TG DPS.  Simulate warrior dps with titan's grip.
Copyright (C) 2008 David Warburton

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
#include "abilities.h"
#include "common.h"

ability_table_type ab_table[] = 
{
//	rage_cost, cooldown, cast_time, miss_count, swing_count,  name
	{ 0,       0,        0,         0,          0,            "Auto Attack"},
	{ 30,      6,        0,         0,          0,            "Blood Thirst"},
	{ 25,      10,       0,         0,          0,            "Whirl Wind"},
	{ 15,      0,        15,        0,          0,            "Slam"},
	{ 12,      0,        0,         0,          0,            "Heroic Strike"}
};


int player::do_WW(int now)
{
	hit_type main_results, off_results;
	int mdmg, odmg, c, r, miss;
	char *main_type, *off_type;

	miss = get_miss(AB_WW);
	ab_table[AB_WW].swing_count++;
	ab_table[AB_WW].swing_count++;
	//Calculate mdmg and main_results
	r = roll(1, 100);
	if (r < miss ) 
	{	main_results = MISS;
		mdmg = 0;
		ab_table[AB_WW].miss_count++;
	}
	else
	{	
		rage -= ab_table[AB_WW].rage_cost;
		c = (r < (miss + get_crit())) ? 2 : 1;
		mdmg = c*(get_ap()*main->speed/140 + roll(main->min, main->max));
		if (c == 2)
		{
			main_results = CRIT;
			flurry_charges = 3;
			rampage_avail = 100;// switch to new version of rampage
			rampage_crit_bonus = 5;
		}
		else
			main_results = HIT;
		
	}
	// END Main Damage
	// OFF swing
	r = roll(1, 100);
	if (r < miss ) 
	{	off_results = MISS;
		odmg = 0;
		ab_table[AB_WW].miss_count++;
	}
	else
	{	c = (r < (miss + get_crit())) ? 2 : 1;
		odmg = c*(get_ap()*off->speed/140 + roll(off->min, off->max));
		if (c == 2)
		{
			off_results = CRIT;
			flurry_charges = 3;
			rampage_avail = 100;// switch to new version of rampage
			rampage_crit_bonus = 5;
		}
		else
			off_results = HIT;
		
	}

	odmg = (int)((float)odmg * .5 * 1.25);

	switch (main_results) {
		case MISS: main_type = "misses";  break;
		case HIT: main_type = "hits"; break;
		case CRIT: main_type = "CRITS";break;
	}
	switch (off_results) {
		case MISS: off_type = "misses";  break;
		case HIT: off_type = "hits"; break;
		case CRIT: off_type = "CRITS"; break;
	}
	if (verbose & OUT_STORY) printf("%-7.7d: WHIRLWIND -- Main %s for %d and Off %s for %d\n", now,main_type, mdmg,off_type, odmg);
	if (verbose & OUT_RAGE) printf("%-7.7d: Rage at %d\n",now, rage);
	WW_cd = 90;
	gcd = 15;
	return (odmg + mdmg);
}
int player::do_slam(int now)
{
	hit_type results;
	char *type;
	int r, dmg, c, miss;

	miss = get_miss(AB_SLAM);
	ab_table[AB_SLAM].swing_count++;
	r =  roll(1, 100);
	if (r < miss)
	{
		results = MISS;
		dmg = 0;
		ab_table[AB_SLAM].miss_count++; 
	}
	else
	{
		rage -= ab_table[AB_SLAM].rage_cost;
		WEAPON w = main;
		int norml_speed_mod = int(w->twohands ? 3.3 : 2.4);
		c = (r < (miss + get_crit())) ? 2 : 1;
		dmg = c*(get_ap()*norml_speed_mod/140 + roll(w->min, w->max));
		results = (c == 2 ? CRIT : HIT);
	}
	switch (results) {
		case MISS: type = "misses"; break;
		case HIT: type = "hits"; break;
		case CRIT: type = "CRITS"; 
			flurry_charges = 3;
			rampage_avail = 100;
			rampage_crit_bonus = 5;
			break;
	}
	if (verbose & OUT_STORY) printf("%-7.7d: INSTA SLAM %s for %d\n", now, type, dmg);
	if (verbose & OUT_RAGE) printf("%-7.7d: Rage at %d\n",now, rage);

	gcd = 15;
	return (dmg);

}
int player::do_BT(int now)
{
	hit_type results;
	char *type;
	int dmg, c, r, miss;
	
	miss = get_miss(AB_BT);
	ab_table[AB_BT].swing_count++;
	r = roll(1, 100);
	if (r < miss)
	{
		results = MISS;
		dmg = 0;
		ab_table[AB_BT].miss_count++;
	}
	else 
	{
		rage -= ab_table[AB_BT].rage_cost;
		c = (r < (miss + get_crit())) ? 2 : 1;
		dmg = c*get_ap()*45/100;
		results = (c == 2 ? CRIT : HIT);
	}
	switch (results) {
		case MISS: type = "misses";  break;
		case HIT: type = "hits"; break;
		case CRIT: 
			type = "CRITS";
			flurry_charges = 3;
			rampage_avail = 100;
			rampage_crit_bonus = 5;
			slam_avail = 50;
			break;
	}
	if (verbose & OUT_STORY) printf("%-7.7d: BLOOD THIRST %s for %d\n", now, type, dmg);
	if (verbose & OUT_RAGE) printf("%-7.7d: Rage at %d\n",now, rage);
	BT_cd = 60;
	gcd = 15;
	return (dmg);
}
int player::autoattack(WEAPON w, hit_type *result)
{
	int c, dmg, miss;
	int r = roll(1, 100);
	
	miss = get_miss(AB_AUTO);
	ab_table[HS ? AB_HS : AB_AUTO].swing_count++;

	if (r < miss) {
		*result = MISS;
		ab_table[HS ? AB_HS : AB_AUTO].miss_count++;
		return 0;
	} 
	c = (r < (miss + get_crit())) ? 2 : 1;
	dmg = c*(get_ap()*w->speed/140 + roll(w->min, w->max));
	if (HS) 
		dmg += c*317;
	if (c == 2)
	{
		*result = CRIT;
		flurry_charges = 3;
		rampage_avail = 100;
		rampage_crit_bonus = 5;
	}
	else
		*result = HIT;
	return dmg;
}
